package com.resouxs.free_book.service;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class ApiImp {

    private Retrofit retrofit;
    private Retrofit retrofitChContent;
    private final static ApiImp sApiImp = new ApiImp();
    private Retrofit retrofitResou;

    private ApiImp() {
        retrofit = initRetrofit(ApiConstant.BASE_URL);
        retrofitChContent = initRetrofit(ApiConstant.BASE_CH_CONTENT_URL);
        retrofitResou = initRetrofit(ApiConstant.BASE_RESOU_URL);
    }

    public static ApiImp getInstance() {
        return sApiImp;
    }

    private Retrofit initRetrofit(String baseUrl) {
        return new Retrofit.Builder().baseUrl(baseUrl)
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .client(provideOkHttpClient())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    /**
     * 提供HTTP客户端
     *
     * @return
     */
    public static OkHttpClient provideOkHttpClient() {
        //http日志拦截器
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        //需要打印出的日志级别
        interceptor.setLevel(HttpLoggingInterceptor.Level.NONE);
        return new OkHttpClient.Builder()
                .addInterceptor(interceptor)
                .build();
    }

    public <T> T getService(Class<T> clazz) {
        return retrofit.create(clazz);
    }

    public <T> T getChContentService(Class<T> clazz) {
        return retrofitChContent.create(clazz);
    }

    public <T> T getResouService(Class<T> clazz) {
        return retrofitResou.create(clazz);
    }



}
